package com.garden.web.interceptor;


import com.garden.utils.DateUtil;
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Document
@Data
public class AccessLog {
    private static ThreadLocal<AccessLog> logThreadLocal = new ThreadLocal<AccessLog>();
    private String id;
    private Date start;
    private Date end;
    private String action;
    private long execTime;
    private String userId;
    private String ip;
    private String sessionId;
    private Map<String, Object> requestParam;
    private String status;
    private String exception;

    private AccessLog() {
        id = DateUtil.timestamp().toString();
        requestParam = new HashMap<String, Object>();
    }

    /**
     * 获取访问日志
     * @return
     */
    public static AccessLog getAccessLog() {
        AccessLog currentAccessLog = logThreadLocal.get();
        if (currentAccessLog == null) {
            currentAccessLog = new AccessLog();
            logThreadLocal.set(currentAccessLog);
        }
        return currentAccessLog;
    }
}
